using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Kingdom_of_the_Dragon
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        SpriteFont font;

        Dragon ship;
        Warrior warrior;
        Camera camera;
        Enemy tower;
        List<Fireball> playerFireballList = new List<Fireball>();
        List<Fireball> enemyFireballList = new List<Fireball>();
        
        KeyboardState kPrevious, kAtual;
        MouseState mPrevious, mAtual; 
        Texture2D playerFireballTexture;
        Texture2D lala;

        bool Switch_timer = true;
        int Time_max = 50;
        int timer;
        

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            Window.Title = "Dragon";
        }

        protected override void Initialize()
        {
            Viewport view = new Viewport();
            view.Width = 800;
            view.Height = 600;
            timer = Time_max;
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            //Carregando texturas
            lala = Content.Load<Texture2D>("tiro_asteroid");
            font = Content.Load<SpriteFont>("SpriteFont1");
            playerFireballTexture = Content.Load<Texture2D>("tiro_asteroid");

            //Instanciando objetos
            camera = new Camera();
            ship = new Dragon(Content.Load<Texture2D>("image_asteroid"), new Vector2(130, 130), 0);
            warrior = new Warrior((Content.Load<Texture2D>("boneco")), new Vector2(130, 130), 0);
            tower = new Enemy(Content.Load<Texture2D>("image_asteroid"), new Vector2(100, 500));
        }

        protected override void UnloadContent()
        { }

        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit();
            kAtual = Keyboard.GetState();
            mAtual = Mouse.GetState();

            if ((mAtual.LeftButton == ButtonState.Pressed))// & (mPrevious.LeftButton = ButtonState.Released))
            {
                Fireball shot = new Fireball(playerFireballTexture,
                               new Vector2(ship.Position.X, ship.Position.Y), 150f, ship.OlhandoDireita ? ship.Angle_direct : ship.Angle_direct + 180);
                playerFireballList.Add(shot);
            }
            for (int i = 0; i < playerFireballList.Count; i++)
            {
                playerFireballList[i].Update(gameTime);
                if (playerFireballList[i].lifetime > 50)
                { playerFireballList.RemoveAt(i); }
            }

            if (Switch_timer == false && timer < Time_max)
            {
                timer++;
            }
            else if (timer >= Time_max)
            {
                Switch_timer = true;
                timer = 0;
            }
            if (Switch_timer)
            {
                Switch_timer = false;
                Fireball shot2 = new Fireball(playerFireballTexture,
                               new Vector2(tower.position.X, tower.position.Y), 150f, tower.angleResult - 180);
                enemyFireballList.Add(shot2);
            }
            for (int i = 0; i < enemyFireballList.Count; i++)
            {
                enemyFireballList[i].Update(gameTime);
                if (enemyFireballList[i].lifetime > 50)
                { enemyFireballList.RemoveAt(i); }
            }

            if (kAtual.IsKeyDown(Keys.Space) && kPrevious.IsKeyUp(Keys.Space))
            {

            }
            ship.Move(kAtual, kPrevious, Keys.W, Keys.S, Keys.A, Keys.D);
            warrior.Mover_Update(kAtual, kPrevious, Keys.W, Keys.S, Keys.A, Keys.D, Keys.Space, ship);
            camera.Move(kAtual, Keys.Up, Keys.Down, Keys.Left, Keys.Right);
            tower.Update(ship.Position);

            kPrevious = kAtual;
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);
            //foreach (Fireball shipshoot in ship.playerFireballList)
            foreach (Fireball shipshoot in playerFireballList)
                shipshoot.Draw(spriteBatch, camera.posorigem);

            //foreach (Fireball towershoot in enemy.enemyFireballList)
            foreach (Fireball towershoot in enemyFireballList)
                towershoot.Draw(spriteBatch, camera.posorigem);


            spriteBatch.Begin();

            for (int i = 0; i < playerFireballList.Count; i++)
            { spriteBatch.DrawString(font, Convert.ToString(playerFireballList[i].Speed) + Convert.ToString(playerFireballList[i].Position), new Vector2(( i * 100), 0), Color.Black);}

            spriteBatch.Draw(lala, new Vector2(camera.posorigem.X + 10, camera.posorigem.Y + 10), Color.White);
            spriteBatch.DrawString(font, "Vida Jogador:" + ship.Life, new Vector2(camera.posorigem.X + 0, camera.posorigem.Y + 0), Color.Black);
            spriteBatch.DrawString(font, "Vida Inimigo:" + tower.vida, new Vector2(camera.posorigem.X + 0, camera.posorigem.Y + 25), Color.Black);
            spriteBatch.DrawString(font, "Tempo do tiro inimigo:" + timer, new Vector2(camera.posorigem.X + 100, camera.posorigem.Y + 100), Color.Black);
            spriteBatch.End();

            tower.Draw(spriteBatch, camera.posorigem);
            ship.Draw(spriteBatch, camera.posorigem);
            warrior.Draw(spriteBatch, camera.posorigem, ship.Angle_direct, ship.Radians_direct);
            base.Draw(gameTime);
        }
    }
}
